package bank.service.spring.employee;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import bank.model.dao.EmployeeDAO;

public class EmployeeController implements Controller{
	private EmployeeDAO employeeDAO;
	public EmployeeController(EmployeeDAO employeeDAO) {
		super();
		this.employeeDAO = employeeDAO;
		
	}
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int id = 0;
		boolean error = false;
		try {
			String sid = request.getParameter("id_type");
			id = Integer.valueOf(sid);		

			if ((sid == null) || (id < 1)) {
				 //throw new Exception("No department selected");
				error = true;
				request.setAttribute("errorFlag", error);
				return new ModelAndView("employee/EmployeeList");
			}		
		} catch (Exception e) {
			error = true;
			request.setAttribute("errorFlag", error);
			e.printStackTrace();
			return new ModelAndView("employee/EmployeeList");			
		}
		List employeeList = employeeDAO.getEmployeeInDepartment(id);
		request.getSession().setAttribute("employee_dept_id", id);
		return new ModelAndView("employee/EmployeeList", "employeeList", employeeList);
	}
	

}
